package com.Calculator.controller.DisplayText;

public class StringNumber implements iStringNumber{
    //字符串数字
    private String stringNumber;
    /**
     * Enter锁
     * true: 刚按完Enter
     */
    private boolean enterLock;
    /**
     * BacK锁
     */
    private boolean backLock;

    public StringNumber() {
        stringNumber = "";
        enterLock = false;
        backLock = false;
    }

    /**
     * 键盘输入数字，拼接到stringNumber上
     * @param keyCode 键盘数据
     */
    @Override
    public void inputNumber(int keyCode) {
        //如果处于Enter锁状态，重置输入
        if (enterLock) {
            reSetNumber();
        }
        stringNumber += toString(keyCode - 96);
        backLock = false;
    }

    /**
     * 复位功能
     */
    @Override
    public void reSetNumber() {
        stringNumber = "";
        enterLock = false;
        backLock = false;
    }

    @Override
    public void backNumber() {
        //删除字符串最后一个字母
        if (!stringNumber.isEmpty()) stringNumber = stringNumber.substring(0, stringNumber.length() - 1);
    }

    /**
     * 切换成小数模式
     */
    @Override
    public void switchNumber() {
        //拼接"."
        if (!isDecimals(stringNumber)) stringNumber += ".";
    }

    @Override
    public void changeNumber() {
        //分为整数，小数两种情况
        if (isDecimals(stringNumber)) stringNumber = toString(-1 * toDouble(stringNumber));
        else stringNumber = toString(-1 * toInt(stringNumber));
    }

    /**
     * 回车功能
     * @param temp 被操作数
     * @param sign 操作符
     */
    @Override
    public void enterNumber(StringNumber temp, String sign) {
        switch (sign) {
            case "+":
                addNumber(temp);
                break;
            case "-":
                subNumber(temp);
                break;
            case "x":
                mulNumber(temp);
                break;
            case "÷":
                divNumber(temp);
                break;
        }
        //判断是小数还是整数
        if (isDecimals(stringNumber)) stringNumber = toString(toDouble(stringNumber));
        else stringNumber = toString(toInt(stringNumber));
        enterLock = true;
        backLock = true;
    }

    /**
     * 加法运算
     * @param temp 加数
     */
    @Override
    public void addNumber(StringNumber temp) {
        double result = toDouble(stringNumber) + temp.toDouble(temp.getStringNumber());
        //若结果为整数，切换成整数模式，其他三个同理
        if (isDecimals(removeZero(toString(result)))) stringNumber = toString(result);
        else stringNumber = removeZero(toString(result));
    }

    /**
     * 减法运算
     * @param temp 减数
     */
    @Override
    public void subNumber(StringNumber temp) {
        double result = toDouble(stringNumber) - temp.toDouble(temp.getStringNumber());
        if (isDecimals(removeZero(toString(result)))) stringNumber = toString(result);
        else stringNumber = removeZero(toString(result));
    }

    /**
     * 乘法运算
     * @param temp 乘数
     */
    @Override
    public void mulNumber(StringNumber temp) {
        double result = toDouble(stringNumber) * temp.toDouble(temp.getStringNumber());
        if (isDecimals(removeZero(toString(result)))) stringNumber = toString(result);
        else stringNumber = removeZero(toString(result));
    }

    /**
     * 除法运算
     * @param temp 除数
     */
    @Override
    public void divNumber(StringNumber temp) {
        double result = toDouble(stringNumber) / temp.toDouble(temp.getStringNumber());
        if (isDecimals(removeZero(toString(result)))) stringNumber = toString(result);
        else stringNumber = removeZero(toString(result));
    }

    /**
     *判断是否是小数
     * true: 是小数
     * false: 不是小数
     * @return 判断结果
     */
    public boolean isDecimals(String str) {
        //判断字符串是否包含“.”
        return str.contains(String.valueOf('.'));
    }

    /**
     * 此方法用去移除字符串数字末尾的零
     * @param stringNumber 字符串数字
     * @return 移除后的数字
     */
    public String removeZero(String stringNumber) {
        for (int i = stringNumber.length() - 1; i >= 0; i--) {
            if (stringNumber.charAt(i) != '0') {
                stringNumber = stringNumber.substring(0, i);
                break;
            }
            if(i == 0) {
                stringNumber = "";
            }
        }
        return stringNumber;
    }

    /**
     * 将数字转换成字符串函数
     * @param num 要转换的数字
     * @return 数字字符串
     */
    public String toString(double num) {
        return num + "";
    }
    public String toString(int num) {
        return num + "";
    }

    /**
     * 将字符串转换成浮点数字函数
     * @param str 要转换的字符串
     * @return 浮点数
     */
    public double toDouble(String str) {
        if (!str.isEmpty()) return Double.parseDouble(str);
        return -1;
    }

    /**
     * 将字符串转换成整形数字函数
     * @param str 要转换的字符串
     * @return 浮点数
     */
    public int toInt(String str) {
        if (!str.isEmpty()) return Integer.parseInt(str);
        return -1;
    }

    //字符串数字获取、设置方法
    public String getStringNumber() {
        return stringNumber;
    }
    public void setStringNumber(String stringNumber) {
        this.stringNumber = stringNumber;
    }

    //Enter锁获取、设置方法
    public boolean isEnterLock() {
        return enterLock;
    }
    public void setEnterLock(boolean enterLock) {
        this.enterLock = enterLock;
    }

    //Back锁获取、设置方法
    public boolean isBackLock() {
        return !backLock;
    }
    public void setBackLock(boolean backLock) {
        this.backLock = backLock;
    }
}
